package com.shmilyou.dissertationmanagement.biz;
/* Created with 岂止是一丝涟漪     530060499@qq.com    2019/2/9 */

import com.shmilyou.dissertationmanagement.domain.SoftwareCopyright;
import com.shmilyou.dissertationmanagement.repository.SoftwareCopyrightRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class SoftwareCopyrightService {

    private final SoftwareCopyrightRepository softwareCopyrightRepository;

    @Autowired
    public SoftwareCopyrightService(SoftwareCopyrightRepository softwareCopyrightRepository) {
        this.softwareCopyrightRepository = softwareCopyrightRepository;
    }

    public SoftwareCopyright queryById(Integer id) {
        Optional<SoftwareCopyright> softwareCopyright = softwareCopyrightRepository.findById(id);
        return softwareCopyright.orElse(null);
    }

    public SoftwareCopyright save(SoftwareCopyright entity) {
        return softwareCopyrightRepository.save(entity);
    }

    public SoftwareCopyright update(SoftwareCopyright entity) {
        return softwareCopyrightRepository.save(entity);
    }

    public SoftwareCopyright delete(SoftwareCopyright entity) {
        entity.setDel(1);
        return softwareCopyrightRepository.save(entity);
    }

    public List<SoftwareCopyright> queryByName(String name) {
        return softwareCopyrightRepository.findAllByName(name);
    }

    public List<SoftwareCopyright> queryAll() {
        return softwareCopyrightRepository.findAllByDel(0);
    }

    public Page<SoftwareCopyright> queryAll(int pageIndex, int pageSize) {
        Pageable page = PageRequest.of(pageIndex, pageSize);
        Page<SoftwareCopyright> resultPage = softwareCopyrightRepository.findAllByDel(page, 0);
        return resultPage;
    }

    public int getCount() {
        return softwareCopyrightRepository.countByDel(0);
    }
}
